var vue = new Vue({
    el: "#app",
    data: {
        title: "MoMo登录",
        errmsg: "",
        user: {
            account: "yykk",
            password: "123456",
            code: ""
        }
    },
    mounted() {
        document.onkeydown = (e) => {
            if (e.keyCode == 13) {
                this.toLogin();
            }
        }
    },
    watch: {
        errmsg: {
            handler(newval, oldval) {
                //如果监听器监听器已经存在了，那么清空计时器，防止用户多次重复点击，累加定时器时效
                if (this.stimer) clearTimeout(this.stimer);
                this.stimer = setTimeout(() => {
                    this.errmsg = "";
                }, 3000)
            }
        }
    },
    methods: {
        toLogin:throttle(function () {
            var that = this;
            var user = that.user;

            if (!user.account) {
                that.errmsg="请输入账号"
                document.getElementById("account").focus();
                return;
            }

            if (!user.password) {
                that.errmsg="请输入密码"
                document.getElementById("pwd").focus();
                return;
            }

            if (!user.code) {
                that.errmsg="请输入验证码"
                document.getElementById("code").focus();
                return;
            }

            axios.post("/logined", user).then(res => {
                if (res.data == "success") {
                    window.location.href = "/collect";
                } else if (res.data == "failcode") {
                    that.errmsg="你的验证码输入有误"
                    //验证码输入错误，清空验证码
                    that.user.code = "";
                    document.getElementById("code").value = "";
                    document.getElementById("code").focus();
                    document.getElementById("ksdimgcode").src = "/kaptcha?d=" + new Date().getTime();
                } else {
                    that.errmsg="输入账号密码有误"
                    that.user.password = "";
                    document.getElementById("pwd").value = "";
                    document.getElementById("pwd").focus();
                }
            })
        },3000)
    }
});


